/****************************************************************************
**
** Copyright (C) 2011 Jesús Martínez Pérez
**
** This program is free software: you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the 
** Free Software Foundation, either version 3 of the License, or (at your
** option) any later version.
**
** This program is distributed in the hope that it will be useful, but 
** WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
** for more details.
**
** You should have received a copy of the GNU General Public License along 
** with this program. If not, see <http://www.gnu.org/licenses/>
**
****************************************************************************/
#include <vd_unsignedint.h>


VD_unsignedint::VD_unsignedint(){
	r=MAX;
	n=0;
	v=new unsigned int[MAX];
}


VD_unsignedint::VD_unsignedint(int tam){
	r=tam;
	n=0;
	v=new unsigned int[tam];
}


VD_unsignedint::VD_unsignedint(const VD_unsignedint &V){
	r=V.r;
	n=V.n;
	v=new unsigned int[r];
	for(int i=0;i<n;i++){
		v[i]=V.v[i];
	}
}


void VD_unsignedint::Ampliar(){
	r*=2;
	unsigned int *aux=new unsigned int [r];
	for(int i=0;i<n;i++){
		aux[i]=v[i];
	}
	delete []v;
	v=aux;
}


VD_unsignedint::~VD_unsignedint(){

}


VD_unsignedint &VD_unsignedint::operator=(const VD_unsignedint &V){
	if(this!= &V){
		delete [] v;
		r=V.r;
		n=V.n;
		v = new unsigned int[r];
		for(int i=0;i<n;i++){
			v[i]=V.v[i];
		}
	}
	return *this;
}


int VD_unsignedint::Size() const {
	return n;
}


void VD_unsignedint::Add(unsigned int a){
	if(n>=r){
		Ampliar();
	}
	v[n]=a;
	n++;
}


unsigned int VD_unsignedint::Get(int i)const{
	return v[i];
}


void VD_unsignedint::Set(int i, unsigned int v){
	if((i>=0)&&(i<=n-1))
		this->v[i]=v;
}


void VD_unsignedint::Borrar(int pos){
	for(int i=pos+1;i<n;i++){
		v[i-1]=v[i];
	}
	n--;
}


bool VD_unsignedint::operator==(VD_unsignedint &vd){
	if(n==vd.n){
		for(int i=0;i<n;i++)
			if(v[i]!=vd.v[i])
				return false;
			return true;
			}else
				return false;
}


bool VD_unsignedint::operator!=(VD_unsignedint &vd){
	return !(*this==vd);
}


VD_unsignedint VD_unsignedint::operator+(unsigned int a){
	VD_unsignedint Vaux(*this);
	Vaux.Add(a);
	return Vaux;
}

VD_unsignedint VD_unsignedint::operator+(VD_unsignedint &v){
	VD_unsignedint Vaux(*this);
	for(int i=0; i<v.Size();i++){
		unsigned char a=v.Get(i);
		Vaux.Add(a);
	}
	return Vaux;
}


unsigned int &VD_unsignedint::operator[](int i){
	return v[i];	
}


VD_unsignedint operator+(unsigned int a,const VD_unsignedint &vd){
	VD_unsignedint Vaux(vd.n+1);
	Vaux.v[0]=a;
	for(int i=0;i<vd.n;i++){
		Vaux.v[i+1]=vd.v[i];
	}
	Vaux.n=vd.n+1;
	return Vaux;
}


ostream & operator<<(ostream &os,const VD_unsignedint &vd){
	os<<vd.n<<"->";
	for(int i=0;i<vd.n;i++){
		os<<vd.v[i]<<" ";
	}
	return os;
}


istream & operator>>(istream &is,VD_unsignedint &vd){
	int n;
	is>>n;
	VD_unsignedint Vaux(n);
	for(int i=0; i<n;i++){
		unsigned int a;
		is>>a;
		Vaux.Add(a);
	}
	vd=Vaux;
	return is;
}
